/*
 *  Copyright 2010 sonrisa.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.zh.jbet.core.service.impl;

import com.zh.jbet.core.dao.GameContextDao;
import com.zh.jbet.core.entity.GameContext;
import com.zh.jbet.core.exception.ExceptionCodesEnum;
import com.zh.jbet.core.exception.SystemException;
import com.zh.jbet.core.service.TreeService;
import com.zh.tree.TreeUtils;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author sonrisa
 */
public class GameContextService extends BaseEntityService<GameContext> implements TreeService<GameContext> {

    // Map<Long, DefaultMutableTreeNode> cache = new HashMap<Long, DefaultMutableTreeNode>();

    @Override
    public void saveRootNode(DefaultMutableTreeNode root) {
        TreeUtils.setNestedIds(root);
        Enumeration<DefaultMutableTreeNode> gameEnum = root.preorderEnumeration();
        DefaultMutableTreeNode actNode, parentNode;
        GameContext actGame;
        Long rootId = null;
        while (gameEnum.hasMoreElements()) {
            actNode = gameEnum.nextElement();
            actGame = (GameContext)((DefaultMutableTreeNode)actNode).getUserObject();
            actGame.setRootId(rootId);
            parentNode = (DefaultMutableTreeNode)actNode.getParent();
            if (parentNode != null) {
                actGame.setParentId(((GameContext)parentNode.getUserObject()).getId());
            }
            actGame = entityDao.save(actGame);
            if (rootId == null) {
                rootId = actGame.getId();
            }
            entityDao.flush();
            actNode.setUserObject(actGame);
        }
    }

    @Override
    public void deleteNode(DefaultMutableTreeNode contextNode) {
        Enumeration<DefaultMutableTreeNode> gameEnum = contextNode.postorderEnumeration();
        GameContext actGame;
        while (gameEnum.hasMoreElements()) {
            actGame = (GameContext)((DefaultMutableTreeNode)gameEnum.nextElement()).getUserObject();
            if (actGame.getId() != null) {
                entityDao.delete(actGame);
                entityDao.flush();
            }
        }
    }

    @Override
    public DefaultMutableTreeNode getRootNodeById(Long rootId) {
        DefaultMutableTreeNode ret = null;
        List<GameContext> gcs = ((GameContextDao)entityDao).findRootGames(rootId);
        return TreeUtils.buildOrderedNestedTree(gcs);
    }

    private static GameContext getParentGame(DefaultMutableTreeNode node) {
        DefaultMutableTreeNode pnode = (DefaultMutableTreeNode)node.getParent();
        return (GameContext)pnode.getUserObject();
    }


}
